//cList: a fast, durable sequencial chained list. by Artheru.

const int maxLeafSize=1024;
const int ListBranchConst=7;

class BadVisit{};
struct IterR{
	bool            Elemental;
	int             UnDone;
	cSeqList<void*> list;
};
template<typename type>
class cListBranch{
public:
	cSeqList<void*> st;
	cSeqList<int>   lT;

	int				level;

	void*			father;
	int				sonID;
	
	int length(){
		return
			lT[0]+lT[1]+lT[2]+lT[3]+lT[4]+lT[5]+lT[6];
	}

	void* visit(int what){
		if (what-=lt[0]<0)
			return st->visit(what+lt[0]);
		if (what-=lt[1]<0)
			return st->visit(what+lt[1]);
		if (what-=lt[2]<0)
			return st->visit(what+lt[2]);
		if (what-=lt[3]<0)
			return st->visit(what+lt[3]);
		if (what-=lt[4]<0)
			return st->visit(what+lt[4]);
		if (what-=lt[5]<0)
			return st->visit(what+lt[5]);
		if (what-=lt[6]<0)
			return st->visit(what+lt[6]);
		throw BadVisit();
	}

	int locateDirectSub(int &num){
		for (int i=0; i<7; ++i){
			if (num<lT[i]) return i;
			else num-=lT[i];
		}
		return -1;
	}

	void push(void* what){//a subBranch  --  Assure do not over push!
		st.push(what);
		lT.push(what->length());
		(cListBranch<type>)what->father=(void*)this;
		(cListBranch<type>)what->sonID=st.length()-1;
	}

	void insertBeforeSub(int which,void* what){//insert a subBranch -- Assure do not over insert!
		st.insertBefore(which,what);
		lT.insertBefore(which,what->length());
		(cListBranch<type>)what->father=(void*)this;
		(cListBranch<type>)what->sonID=st.length()-1;
	}
	
	void insertBeforeSubs(int which,cSeqList<void*>* what){//insert subBranchs -- Assure do not over insert!
		st.insertBefore(which,what);
		for (int i=which; i<st->length(); ++i){
			(cListBranch<type>*)(st[i])->father=(void*)this;
			(cListBranch<type>*)(st[i])->sonID=i;
			
		}
	}

	void insertBeforeSubs(int which,cSeqList<void*>* what,int count){//insert subBranchs -- Assure do not over insert!
		st.insertBefore(which,what,count);
		lT.expand(count);
		for (int i=which; i<st->length(); ++i){
			(cListBranch<type>*)(st[i])->father=(void*)this;
			(cListBranch<type>*)(st[i])->sonID=i;
			lT[i]=(cListBranch<type>*)(st[i])->length();
		}
	}

	void updateLt(int thisSize){
		(cListBranch<type>*)father->lT[sonID]
	}

	sub* insertBeforeDirectSubBranch(int place, cSeqList<void*>* what){

		if (what->length()<=7-st.length()){//if Filling OK?
			insertBeforeSubs(place,what); //Fill
		};
		//Not OK:
		int i=0;
		//1.put back
		for (int k=place; k<st.length(); ++k)
			what->push(st[k]);
		//2.sequecial put in.
		for (int k=place; k<st.length(); ++k, ++i)
			st[k]=what->visit(i);
		//3.full? expand.
		for (;st.length()<7; ++i)
			st.push(what->visit(i));
		//4.size limit reached:
		cSeqList< void* > ls;     //PokerPacekts box
		while(i<what->length()){
			cListBranch<type> *tmp=new cListBranch<type>; //Create a Poker Packet
			tmp->level=level; //Same level!
			while(i<what->length() && tmp->st->length()<7) 
				tmp->push(what->visit(i++));  //poker is pushed into this Packet.
			ls.push((void*)tmp); //Packet ready, Into the Poker Packet Box.
		};
		(cListBranch<type>)father->insertBeforeDirectSubBranch(sonID+1, &ls); //give the pokers to father.
	}

	
	sub* insertBefore(int place, int count, type* what){
		int which=locateDirectSub(place);
		
		
		for(;which<7;++which){
			if (what-=lt[which]<0){//Insert Place
				IterR<type> R=(cListBranch<type>)st->insertBefore(what+lt[which],count,what);
				if (R.Elemental){//leaf not finished.
					//1.Create a Poker Pile:
					int leaves=R.UnDone/maxLeafSize+(R.Undone%maxLeafSize>0?1:0);
					int from=count-R.UnDone,
						tCount=R.UnDone;
					cSeqList< cSeqList<type>* > lists;
					for(int i=0; i<leaves; ++i){
						tCount-=maxLeafSize;
						lists.push(new cSeqList<type>(PTRCopy,tCount>maxLeafSize?maxLeafSize:tCount,what+from)); //Create Poker
						from+=maxLeafSize;
					};
					//2.Pile over.
					int pile=which+1,
						PokerNum=0;
					for(;PokerNum<lists.length() && st.length()<ListBranchConst; ++PokerNum){
						st.insertBefore(pile,(void*)(lists[i]));
					};
					if (PokerNum<lists.length() && st.length()==ListBranchConst){//Require Shifting to upper layer
						cSeqList<void*> *PokerPocket=new cSeqList<void*>;
						int n=0;
						do{
							if (
							PokerPocket->push((void*)
						for(;PokerNum<lists.length();++PokerNum){
						}
						
					};
					return IterR(false,0,NULL);
				}
				else if(R.Undone){//branch not finished.
					
					
			};
		}
		
	}
	


template<typename type>
class cList{
	void* st;
	int   size;
	int   space;
	int   level;
	
	void generatePile(cSeqList<void*> pile, const int& count, const type* what){
		int done=0;
		while(1){
			cSeqList<type> *poker=new cSeqList<type>(maxLeafSize);
			if(done+maxLeafSize<count){
				poker->push(what,maxLeafSize);
				done+=maxLeafSize;
				pile.push((void*)poker);
			}
			else{
				poker->push(what,count-done);
				pile.push((void*)poker);
				break;
			}
		};	
	}

	void insertBefore(const int& place, const int& count, const type* what,){
		if (place==length())
			push(what,count); //if insertion happens at back.
		if (level==0){
			if (size+count<maxLeafSize){
				//Level 0, simply use sequence list.
				++size;
				return (cSeqList*)st->insertBefore(place, what);
			}
			else{
				//big...
				//1.generate a pile
				cSeqList<void*> pile;
				//2.slice.
				cSeqList<type>* last=(cSeqList*)st->slice(place);
				//3.front part into pile:
				pile.push((void*)st);
				//4.generate insertion pile:
				generatePile(pile,count,what);
				//5.sliced last part is been pushed into the pile.
				pile.push((void*)last);
				//6.create the 1st Branch List:
				cListBranch<void*> *tmp=new cListBranch<void*>;
				tmp->level=1;
				st=tmp;
				//7.throw the pile up.
				tmp->insertBeforeDirectSubBranch(0,&pile)
			};
		};
		//Level 1,2,3...
		int remain=place;
		cListBranch<type>* target=(cListBranch<type>*)st;
		while (target->level>1){
			target=(cListBranch<type>*)(target->visit(target->locateDirectSub(remain)));
		};  //Now Target stores SeqLists storing direct information.
		int targetSeq=target->locateDirectSub(remain);//targetSeq:Seqlist, remain:where to insert, target: branch.
		int lengthSeq=((cSeqList<type>)(target->st[targetSeq]))->length();//Length of SeqList.
		//possible A:enough to fill in a single Seqlist:
		if (count+lengthSeq<maxLeafSize){
			(cSeqList<type>)((cListBranch<type>)target->st[targetSeq])->insertBefore(remain,what,count);
		}
		else{
		//possible B: not enough..
			//idea, form a pile, then use insert sub method.
			//1.make piles
			cSeqList<void*> pile;
			generatePile(pile,count,what);
			//2.slice target seqList, push into pile.
			cSeqList<type>* last=(cSeqList<type>)((cListBranch<type>)target->st[targetSeq])->slice(remain);
			pile.push((void*)last);
			//3.throw the pile up.
			target->insertBeforeDirectSubBranch(targetSeq,&pile);
		}
	};
	void newLevel(){
		void* tmp=st;
		st=new cList